package serv;

import org.xmldb.api.*;
import org.xmldb.api.base.*;
import org.exist.xmldb.*;
import java.net.*;

public class Main {
	public static void main(String[] args) {		
		ServerSocket sSocket;
		
		try {
			Class cl = Class.forName("org.exist.xmldb.DatabaseImpl");
			Database database = (Database) cl.newInstance();
			database.setProperty("create-database", "true");
			DatabaseManager.registerDatabase(database);
			
			sSocket = new ServerSocket(6666);
			
			while (true) {
				new ServerSocketThread(sSocket.accept()).start();
				System.out.println("Conexão feita");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				// Fecha o banco de dados.
				Collection col = DatabaseManager.getCollection("xmldb:exist:///db", "admin", "");
				DatabaseInstanceManager manager = (DatabaseInstanceManager)col.getService("DatabaseInstanceManager", "1.0"); 
				manager.shutdown();
			}
			catch (XMLDBException xmldbe){
				xmldbe.printStackTrace();
			}
		}
	}

}
